package com.jf.mall.utils;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidDataSourceFactory;

import javax.sql.DataSource;
import java.sql.*;
import java.util.Properties;

/**
 * Created with IntelliJ IDEA
 * Description:JDBCUtilsByDruid开发
 * User:22954
 * Date:2024-09-13
 * Time:18:51
 */
public class JDBCUtilsByDruid {
    private static final ThreadLocal<Connection> threadLocalConnection = new ThreadLocal<>();
    //数据源对象
    private static DataSource dataSource = null;

    static {
        try {
            Properties properties = new Properties();
            properties.load(JDBCUtilsByDruid.class.getResourceAsStream("/druid.properties"));//
            dataSource = DruidDataSourceFactory.createDataSource(properties);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 获得数据源对象
     *
     * @return DataSource对象
     */
    public static DataSource getDataSource() {
        return dataSource;
    }

    /**
     * 从当前Thread的ThreadLocal获得数据库连接Connection对象. 若ThreadLocal未存储
     * Connection对象,从Druid连接池获得数据库连接Connection对象,并存储于当前Thread的
     * ThreadLocal中
     *
     * @return Connection对象
     */
    public static Connection getConnection() {
        Connection connection = threadLocalConnection.get();
        try {
            if (connection == null) {
                connection = dataSource.getConnection();
                connection.setAutoCommit(false);
                threadLocalConnection.set(connection);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return connection;
    }

    /**
     * 从当前Thread的ThreadLocal获得数据库连接Connection对象,提交事务,释放连接
     * 并清除ThreadLocal缓存
     */
    public static void commit() {
        Connection connection = threadLocalConnection.get();
        if (connection != null) {
            try {
                connection.commit();
            } catch (SQLException e) {
                e.printStackTrace();
            } finally {
                release(connection);
                threadLocalConnection.remove();
            }
        }
    }

    /**
     * 从当前Thread的ThreadLocal获得数据库连接Connection对象,回滚所有事务,释放连接
     * 并清除ThreadLocal缓存
     */
    public static void rollback() {
        Connection connection = threadLocalConnection.get();
        if (connection != null) {
            try {
                connection.rollback();
            } catch (SQLException e) {
                e.printStackTrace();
            } finally {
                release(connection);
                threadLocalConnection.remove();
            }
        }
    }

    /**
     * 将数据库连接对象放回Druid连接池
     *
     * @param connection Connection-数据库连接对象
     */
    public static void release(Connection connection) {
        release(connection, null, null);
    }

    /**
     * 将数据库连接对象放回Druid连接池
     *
     * @param connection Connection-数据库连接对象
     * @param statement  Statement-SQL语句执行器
     */
    public static void release(Connection connection, Statement statement) {
        release(connection, statement, null);
    }

    /**
     * 将数据库连接对象放回Druid连接池
     *
     * @param connection Connection-数据库连接对象
     * @param statement  Statement-SQL语句执行器
     * @param resultSet  ResultSet-结果集
     */
    public static void release(Connection connection, Statement statement, ResultSet resultSet) {
        if (connection != null) {
            try {
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (statement != null) {
            try {
                statement.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (resultSet != null) {
            try {
                resultSet.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 关闭Druid连接池,断开与MySQL的连接
     */
    public static void close() {
        if (dataSource != null) {
            ((DruidDataSource) dataSource).close();
        }
    }
}
